using System;
using Consts;
using Dto;
using UI.Msg;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class CreatePanel : UIBase
    {
        private InputField _inputName;
        private Button _btnCreate;
        private PromptMsg _promptMsg;


        private void Awake()
        {
            Bind(UIEvent.CREATE_PANEL_ACTIVE);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.CREATE_PANEL_ACTIVE:
                    setPanelActive((bool)message);
                    break;
                default:
                    break;
            }
        }

        void Start()
        {
            _inputName = transform.Find("InputName").GetComponent<InputField>();
            _btnCreate = transform.Find("BtnCreate").GetComponent<Button>();
            _btnCreate.onClick.AddListener(CreateClick);
            _promptMsg = new PromptMsg();

        }

        private void CreateClick()
        {
            if (string.IsNullOrEmpty(_inputName.text))
            {
                // 非法输入
                _promptMsg.Change("昵称不能为null",Color.red);
                Dispatch(AreaCode.UI,UIEvent.PROMPT_MSG,_promptMsg);
                return;
            }
            ReqBase _reqBase = new ReqBase(SocketConst.ACCOUNT, AccountCodeConst.SET_NAME,_inputName.text);
            Dispatch(AreaCode.NET, 0, _reqBase);
        }
    }
}